package thread;

import org.junit.Test;

import java.util.*;
import java.util.concurrent.*;

/**
 * @author: feiwang_6
 * @create: 2019/10/31 8:01
 * @description:
 */
public class CollectionTest{
    public static void main(String[] args) {
        ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
        map.put("1", 10);
    }
    
    @Test
    public void forkJoin(){
        
    }
    
    @Test
    public void testDelayQueue() throws InterruptedException {
        long startTime = System.currentTimeMillis();
        DelayQueue<Delayed> delayeds = new DelayQueue<>();
        Delayed delayed = new Delayed() {
            @Override
            public long getDelay(TimeUnit unit) {
                long time = TimeUnit.NANOSECONDS.convert(5, TimeUnit.SECONDS) 
                        + TimeUnit.NANOSECONDS.convert(startTime, TimeUnit.MILLISECONDS)
                        - TimeUnit.NANOSECONDS.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
                return time;
            }

            @Override
            public int compareTo(Delayed o) {
                return 0;
            }
        };

        delayeds.add(delayed);
        System.out.println(startTime);
        System.out.println(delayeds.take());
        System.out.println(System.currentTimeMillis());
    }
    
    @Test
    public void blockingQueue(){
        ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue(10);
        
        
        LinkedBlockingQueue<Integer> linkedBlockingQueue = new LinkedBlockingQueue<>();

        PriorityBlockingQueue<Integer> priorityBlockingQueue = new PriorityBlockingQueue<>();

        DelayQueue<Delayed> delayeds = new DelayQueue<>();

        SynchronousQueue<Integer> synchronousQueue = new SynchronousQueue<>();
        synchronousQueue.add(100);

        LinkedTransferQueue<Integer> linkedTransferQueue = new LinkedTransferQueue<>();
        linkedTransferQueue.put(100);

        LinkedBlockingDeque<Integer> deque = new LinkedBlockingDeque<Integer>();
        deque.add(100);

    }

    @Test
    public void linkedBlockingDeque() throws InterruptedException {
        LinkedBlockingDeque<Integer> deque = new LinkedBlockingDeque<Integer>(2);
        deque.put(100);
        deque.add(10);
        //deque.add(10);
        //deque.add(120);
        //deque.add(121);
        System.out.println(deque);
    }
    
    
    @Test
    public void testLinkedTransferQueue() throws Exception {
        LinkedTransferQueue<Integer> linkedTransferQueue = new LinkedTransferQueue<>();
        linkedTransferQueue.put(100);
        System.out.println(linkedTransferQueue.take());
        System.out.println(linkedTransferQueue.tryTransfer(100));
        linkedTransferQueue.transfer(100);
    }
    
    
    @Test
    public void testSynchronousQueue() throws InterruptedException {
        SynchronousQueue<Integer> synchronousQueue = new SynchronousQueue<>();
        Thread thread = new Thread(() -> {
            try {
                System.out.println("进入获取元素。。。。。");
                Integer take = synchronousQueue.take();
                System.out.println("结束获取数据... " + take);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        
        System.out.println("设置元素。。。。。");
        synchronousQueue.put(1000);
        thread.start();
        Thread.sleep(3000);
        
        thread.join();
    }
    
    @Test
    public void testConcurrentLinkedQueue(){
        ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue();
        queue.add(100);
        queue.add(100);
        queue.add(100);
        queue.poll();
        queue.remove(100);
        System.out.println(queue);
    }
    
    @Test
    public void testConcurrentHahMap(){
        ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>();
        map.put(10, "10");
        map.put(20, "20");
        map.get(10);
        map.size();
        map.remove(10);
        System.out.println(Integer.parseInt("0001111", 2) & 15);
        System.out.println(Integer.parseInt("0011111", 2) & 15);
        System.out.println(Integer.parseInt("0111111", 2) & 15);
        System.out.println(Integer.parseInt("1111111", 2) & 15);
    }
    
    
    @Test
    public void testPriorityQueue(){
        //Comparator<Integer> comparator = (Integer o1, Integer o2) -> o1 >= o2 ? 1 : 0;
        PriorityQueue<Integer> queue = new PriorityQueue<>();
        queue.add(10);
        queue.add(1);
        queue.add(-1);
        System.out.println(queue);
        System.out.println(queue.poll());
        System.out.println(queue);
        
    }
    
    @Test
    public void testVector(){
        Vector<Integer> vector = new Vector<>();
        for(int i = 0; i < 100000; i++){
            vector.add(i);
        }
    }
    
    @Test
    public void testHashMap(){
        HashMap<Integer, String> map = new HashMap<>();
        map.put(7, "");
        map.put(11, "");
        map.put(43, "");
        map.put(59, "");
        map.put(19, "");
        map.put(3, "");
        map.put(35, "");
        map.put(19, "");
        map.remove(12);
        System.out.println(map.keySet());
    }
    @Test
    public void testLinkedHashMap(){
        LinkedHashMap<Integer, String> map = new LinkedHashMap<>(16, 0.75f, true);
        //LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
        map.put(7, "7");
        map.put(11, "11");
        map.put(43, "43");
        map.put(59, "59");
        map.put(19, "19");
        map.put(3, "3");
        map.put(35, "35");
        map.put(19, "19");
        System.out.println(map.get(7));
        System.out.println(map.keySet());
    }

    @Test
    public void testLinkedList(){
        LinkedList<Integer> list = new LinkedList<>();
        
    }
    
    @Test
    public void testHashSet(){
        HashSet<Integer> set = new HashSet<>();
        System.out.println(set.add(12));
        System.out.println(set.add(12));
    }
    
    @Test
    public void testTreeMap(){
        TreeMap<Integer, String> map = new TreeMap<>();
        map.put(7, "");
        map.put(11, "");
        map.put(43, "");
        map.put(59, "");
        map.put(19, "");
        map.put(3, "");
        map.put(35, "");
        System.out.println(map);
        System.out.println(map.lowerEntry(18));
        System.out.println(map.higherEntry(18));
        System.out.println(map.floorEntry(18));
    }

    @Test
    public void testTreeSet(){
        TreeSet<Integer> set = new TreeSet<>();
        set.add(10);
    }
    
    public static ListNode swatchNode(ListNode head){
        if(head == null || head.next == null){
            return head;
        }
        ListNode temp = head.next;
        head.next = swatchNode(temp.next);
        temp.next = head;
        return temp;
    }
    
    public static ListNode swatchNodeWhile(ListNode head){
        ListNode result = head.next;
        head.next = result.next;
        result.next = head;
        while (head.next != null && head.next.next != null){
            ListNode one = head.next;
            ListNode oneNext = head.next.next;
            head.next = oneNext;
            one.next = oneNext.next;
            oneNext.next = one;
            head = one;
        }
        return result;
    }
}
